package com.autonavi.minimap.widget;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.autonavi.minimap.util.cache.BaseLoadListener;
import com.autonavi.minimap.util.cache.CacheWorker.Builder;

/**
 * 网络读取图片的组件
 * 
 * @author mark
 * 
 */
public class WebImageView extends android.widget.FrameLayout {

	private RecyclingImageView mImageView;
	private ProgressBar mProgressBar;

	public WebImageView(Context context) {
		super(context);
		init(context);
	}

	public WebImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		int padding = attrs.getAttributeIntValue("android", "padding", 1);
		setPadding(padding, padding, padding, padding);
		// imageURL = attrs.getAttributeValue(null, "src");
		init(context);

	}

	private void init(Context context) {
		final int width = Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO ? FrameLayout.LayoutParams.MATCH_PARENT
				: FrameLayout.LayoutParams.FILL_PARENT;
		int height = width;
		FrameLayout.LayoutParams LP_FF = new FrameLayout.LayoutParams(width,
				height);
		mImageView = new RecyclingImageView(context);
		// mImageView.setVisibility(android.view.View.GONE);
		mImageView.setLayoutParams(LP_FF);
		addView(mImageView);

		// int
		// processBarwidth=com.autonavi.minimap.util.ResUtil.dipToPixel(mContext,
		// 45);
		FrameLayout.LayoutParams lpPb = new FrameLayout.LayoutParams(
				FrameLayout.LayoutParams.WRAP_CONTENT,
				FrameLayout.LayoutParams.WRAP_CONTENT,
				android.view.Gravity.CENTER);

		mProgressBar = new ProgressBar(context, null,
				android.R.attr.progressBarStyleSmall);
		mProgressBar.setLayoutParams(lpPb);
		// int padding = com.autonavi.minimap.util.ResUtil.dipToPixel(
		// getContext(), 15);
		int padding = (int) TypedValue.applyDimension(
				TypedValue.COMPLEX_UNIT_DIP, 15, context.getResources()
						.getDisplayMetrics());

		mProgressBar.setPadding(padding, padding, padding, padding);
		// setBackgroundColor(0xbaffffff);
		addView(mProgressBar);
		// mProgressBar.setVisibility(View.GONE);

	}

	/**
	 * Must call in UI Thread.
	 * 
	 * @param enable
	 */
	public void setProgressBar(boolean enable) {
		mProgressBar.setVisibility(enable ? View.VISIBLE : View.GONE);
		mImageView.setVisibility(enable ? View.GONE : View.VISIBLE);
	}

	public ImageView getImageView() {
		return mImageView;
	}
	
	public MyLoadingListener getLoadingListener(){
		return new MyLoadingListener(this);
	}

	private class MyLoadingListener extends BaseLoadListener {
		WebImageView webImageView;

		public MyLoadingListener(WebImageView webImageView) {
			this.webImageView = webImageView;
			setProgressBar(false);
		}

		@Override
		public void onStart() {
			webImageView.post(new Runnable() {

				@Override
				public void run() {
					setProgressBar(true);
				}
			});
		}

		@Override
		public void onFinish(final ImageView imageView, final BitmapDrawable drawable,
				Builder cacheParams, boolean isCached) {
			webImageView.post(new Runnable() {

				@Override
				public void run() {
					setProgressBar(false);
					imageView.setImageDrawable(drawable);
				}
			});
		}
		
		@Override
		public void onError() {
			Log.e("DDAI", "onError");
			webImageView.post(new Runnable() {

				@Override
				public void run() {
					setProgressBar(false);
				}
			});
		}
		
	}

}
